package com.zjw.zy.coreui.fragment.tips

import android.os.Bundle
import android.util.Log
import android.view.View
import android.widget.TextView
import androidx.fragment.app.viewModels
import com.zjw.zy.R
import com.zjw.zy.base.BaseFragment
import com.zjw.zy.ext.goWithTopAnim
import com.zjw.zy.viewmodel.TipsViewModel

/**
 * @author     ：zhong.jw
 * @date       ：Created in 2022/11/2 14:36
 * @description：总的数量
 */
class TipTotalFragment : BaseFragment() {

    companion object {
        private const val TAG = "TipTotalFragment"
    }

    private val mTipViewModel: TipsViewModel by viewModels(ownerProducer = {
        requireParentFragment()
    })

    private val mToday: View by lazy { findViewById(R.id.fragment_tip_group_today) }
    private val mTodayNumber: TextView by lazy { findViewById(R.id.fragment_tip_group_today_number) }
    private val mPlan: View by lazy { findViewById(R.id.fragment_tip_group_plan) }
    private val mPlanNumber: TextView by lazy { findViewById(R.id.fragment_tip_group_plan_number) }
    private val mDone: View by lazy { findViewById(R.id.fragment_tip_group_done) }
    private val mDoneNumber: TextView by lazy { findViewById(R.id.fragment_tip_group_done_number) }
    private val mTag: View by lazy { findViewById(R.id.fragment_tip_group_tag) }
    private val mTagNumber: TextView by lazy { findViewById(R.id.fragment_tip_group_tag_number) }
    private val mAdd: TextView by lazy { findViewById(R.id.fragment_tip_add) }

    override fun onPageEnter() {
        mTipViewModel.requireCount()
    }

    override fun onViewRestore(savedInstanceState: Bundle?) {

    }

    override fun onSubscribe() {
        mTipViewModel.getTodayCount().observe(this) {
            mTodayNumber.text = it.toString()
        }
        mTipViewModel.getPlanCount().observe(this) {
            mPlanNumber.text = it.toString()
        }

        mTipViewModel.getDoneCount().observe(this) {
            mDoneNumber.text = it.toString()
        }

        mTipViewModel.getTagCount().observe(this) {
            mTagNumber.text = it.toString()
        }
    }

    override fun onBindViewListener() {
        mAdd.setOnClickListener {
            goWithTopAnim(R.id.nav_tips_edit_fragment)
        }

        mToday.setOnClickListener {
            mTipViewModel.requireTodayTips()
            jumpToList()
        }

        mPlan.setOnClickListener {
            mTipViewModel.requirePlanTips()
            jumpToList()
        }

        mDone.setOnClickListener {
            mTipViewModel.requireDoneTips()
            jumpToList()
        }

        mTag.setOnClickListener {
            jumpToList()
        }
    }

    override fun onBindViewValue() {

    }

    override fun onFindView(rootView: View?) {

    }

    override fun onLayoutId(): Int {
        return R.layout.fragment_total_tips
    }

    override fun onPageExit() {
    }


    private fun jumpToList() {
        val tipFragment = requireParentFragment() as? TipFragment
        tipFragment?.switchPage(TipFragment.TIP_LIST_INDEX) ?: run {
            Log.e(TAG, "jumpToList: error!")
        }
    }
}